package com.aaa.member.controller;

import com.aaa.common.constant.ExceptionConstant;
import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.member.entity.Invoice;
import com.aaa.member.service.InvoiceService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 发票表
 * (Invoice)表控制层
 *
 * @author makejava
 * @since 2020-11-23 10:09:51
 */
@RestController
@RequestMapping("invoice")
public class InvoiceController {
    /**
     * 服务对象
     */
    @Resource
    private InvoiceService invoiceService;

    /**
     * 查询全部发票信息
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("queryInvoice")
    public Result queryInvoice(Integer pageNo, Integer pageSize, String username, Date addtime, String typename){
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        return invoiceService.queryAll(pageNo,pageSize,username,addtime,typename);
    }

    /**
     * 修改发票信息
     * @param invoice
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody Invoice invoice){
        //当ID空为时抛出自定义异常
        if (invoice.getId()==null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.invoiceService.update(invoice));
    }

    /**
     * 增加发票信息
     * @param invoice
     * @return
     */
    @PostMapping("insert")
    public Result inset(@RequestBody Invoice invoice) {
        //判断新增的角色名字为空抛出异常
        if (invoice.getOrderId() == null) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        } else {
            //返回成功
            return new Result(ReturnCode.SUCCESSS.getCode(),
                    ReturnCode.SUCCESSS.getMsg(), this.invoiceService.insert(invoice));
        }
    }

    /**
     * 删除发票信息
     * @param id
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer id){
        //当ID空为时抛出自定义异常
        if (null == id) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.invoiceService.deleteById(id));
    }

    /**
     * 统计
     * @return
     */
    @GetMapping("selectSta")
    public List<Map> selectSta(){
        return invoiceService.selectSta();

    }

}